<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>系统管理</el-breadcrumb-item>
			<el-breadcrumb-item>系统用户管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="用户名">
					<el-input v-model="queryInfo.rootName" clearable @clear="queryList" clearable
						placeholder="请输入用户名"></el-input>
				</el-form-item>
				<el-form-item label="角色">
					<!-- <el-input v-model="queryInfo.rootRole" clearable @clear="queryList" clearable
						placeholder="请输入角色"></el-input> -->
					<el-select v-model="queryInfo.rootRole" clearable @click="queryList" filterable placeholder="请选择角色">
						<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddRootUser">新增系统用户</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 表格区域 -->
		<el-table :data="rootUserList" border style="width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="ID"></el-table-column>
			<el-table-column prop="rootName" label="用户名">
			</el-table-column>
			<el-table-column prop="rootNickname" label="昵称">
			</el-table-column>
			<el-table-column prop="rootEmail" label="邮箱">
			</el-table-column>
			<el-table-column prop="rootPhone" label="手机号">
			</el-table-column>
			<el-table-column prop="rootRole" label="角色">
				<template slot-scope='scope'><span v-if="scope.row.rootRole==1">管理员</span>
					<span v-else-if="scope.row.rootRole==2">咨询师</span>
					<span v-else-if="scope.row.rootRole==3">客服</span></template>
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toEditRootUser(scope.row)">编辑</el-link>
					<el-link type="primary" @click="deleteRootUser(scope.row.id)">删除</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 新增和修改的对话框 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="rootUser">
				<el-form-item label="用户名" label-width="80px">
					<el-input placeholder="请输入用户名" v-model="rootUser.rootName"></el-input>
				</el-form-item>
				<el-form-item label="密码" label-width="80px">
					<el-input placeholder="请输入密码" v-model="rootUser.rootPassword"></el-input>
				</el-form-item>
				<el-form-item label="昵称" label-width="80px">
					<el-input placeholder="请输入昵称" v-model="rootUser.rootNickname"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" label-width="80px">
					<el-input placeholder="请输入邮箱" v-model="rootUser.rootEmail"> </el-input>
				</el-form-item>
				<el-form-item label="手机号" label-width="80px">
					<el-input placeholder="请输入手机号" v-model="rootUser.rootPhone"> </el-input>
				</el-form-item>
				<el-form-item label="角色" label-width="80px">
					<!-- <el-input placeholder="请选择角色" v-model="rootUser.rootRole"> </el-input> -->
					<el-select v-model="rootUser.rootRole" clearable filterable placeholder="请选择角色">
						<el-option v-for="item in options" :key="item.value" :label="item.label"
							:value="item.value"></el-option></el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="addRootUser">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[1,5, 10, 15, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				options: [{
					value: 1,
					label: '管理员'
				}, {
					value: 2,
					label: '咨询师'
				}, {
					value: 3,
					label: '客服'
				}],
				// 查询信息对象
				queryInfo: {
					rootName: '',
					rootRole: '',
					pageNum: 1,
					pageSize: 5
				},
				total: 0,
				rootUserList: [],
				title: '', // 对话框标题
				dialogFormVisible: false, // 是否显示对话框
				rootUser: { // 系统用户对象
					"id": "",
					"rootName": "",
					"rootPassword": "",
					"rootNickname": "",
					"rootEmail": "",
					"rootPhone": "",
					"rootRole": ""
				}
			}
		},
		methods: {
			// 删除系统用户 
			deleteRootUser(id) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('rootUser/' + id)
							.then(resp => {
								if (resp.data.status == 200) {
									this.$message.success(resp.data.message)
									this.queryList()
								} else {
									this.$message.error(resp.data.message)
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},
			// 进入修改的预览数据页面
			toEditRootUser(row) {
				//console.log(row)
				// 赋值
				this.rootUser = row;
				// 设置标题
				this.title = "修改系统用户";
				// 开启对话框
				this.dialogFormVisible = true;
			},
			/// 取消对话框
			cancle() {
				this.dialogFormVisible = false;
				this.rootUser = { // 系统用户对象
						"id": "",
						"rootName": "",
						"rootPassword": "",
						"rootNickname": '',
						"rootEmail": '',
						"rootPhone": '',
						"rootRole": ''
					},
					this.queryList()
			},

			// 进入添加的对话框
			toAddRootUser() {
				this.title = "新增系统用户";
				this.dialogFormVisible = true;
			},
			// 添加系统用户
			addRootUser() {
				if (this.rootUser.id) { // 有值为true  修改
					this.$http.put("rootUser", this.rootUser)
						.then(res => {
							if (res.data.status == 200) {
								// 消息提示
								this.$message.success('修改成功!');
								// 清空数据
								this.rootUser = { // 系统用户对象
										"id": '',
										"rootName": '',
										"rootPassword": '',
										"rootNickname": '',
										"rootEmail": '',
										"rootPhone": '',
										"rootRole": ''
									},
									// 关闭对话框
									this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				} else {
					this.$http.post("rootUser", this.rootUser)
						.then(res => {
							if (res.data.status == 200) {
								// 消息提示
								this.$message.success('添加成功!');
								// 清空数据
								this.rootUser = { // 系统用户对象
									id: '',
									rootName: '',
									rootPassword: '',
									rootNickname: '',
									rootEmail: '',
									rootPhone: '',
									rootRole: ''
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				}
			},
			// 分页查询所有的数据
			queryList() {
				//调用后端接口
				this.$http.get('rootUser/findByPage', {
						params: this.queryInfo
					})
					.then(res => {
						// console.log(res)
						if (res.data.status == 200) {
							this.rootUserList = res.data.data.list
							this.total = res.data.data.total
						} else {
							this.$message.error("查询失败")
						}
					})

			},
			handleSizeChange(size) {
				// console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size
				this.queryInfo.pageNum = 1
				this.queryList()
			},
			handleCurrentChange(num) {
				// console.log('页码发生改变');
				this.queryInfo.pageNum = num
				this.queryList()
			}
		},
		mounted() {
			this.queryList();
		},
	}
</script>
<style>
	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}
</style>